package cn.com.dhcc.turbo.dao.registry;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
@Service
public class NodePermissionDao extends BaseDao{

	@Autowired
	NodeInfoDao nodeInfoDao;
	/**
	 * 获取父节点下的子节点列表
	 * @param currentNode
	 * @return
	 * @throws DaoException
	 */
	public List<NodeInfo> getChildNodes(NodeInfo currentNode) throws DaoException{
		String sql = "select * from "+SI_TABLE("turbo_node_info ")+" t where t.pid=?";
		try {
			return getDao().listModule("获取父节点下的子节点列表", sql, NodeInfo.class, new Object[]{currentNode.getId()});
		} catch (DaoException e) {
			throw new DaoException(e);
		}
	}
	/**
	 * 获取本节点下的子节点列表
	 * @param currentNode
	 * @return
	 * @throws DaoException
	 */
	public List<NodeInfo> getChildNodes() throws DaoException{
		NodeInfo localNode = nodeInfoDao.getLocalNode();
		return getChildNodes(localNode);
	}
	/**
	 * 获取当前节点的父节点
	 * @param currentNode
	 * @return
	 * @throws DaoException
	 */
	public NodeInfo getParentNode(NodeInfo currentNode) throws DaoException {
		String sql = "select t2.* from "+SI_TABLE("turbo_node_info ")+" t1,"+SI_TABLE("turbo_node_info ")+" t2 where t1.pid=t2.id and t1.id=?";
		try {
			return getDao().getModule("获取父节点", sql, NodeInfo.class, new Object[]{currentNode.getId()});
		} catch (DaoException e) {
			throw new DaoException(e);
		}
	}
	/**
	 * 获取本地节点的父节点
	 * @param currentNode
	 * @return
	 * @throws DaoException
	 */
	public NodeInfo getParentNode() throws DaoException {
		NodeInfo localNode = nodeInfoDao.getLocalNode();
		String sql = "select t2.* from "+SI_TABLE("turbo_node_info ")+" t1,"+SI_TABLE("turbo_node_info ")+" t2 where t1.pid=t2.id and t1.id=?";
		try {
			return getDao().getModule("获取父节点", sql, NodeInfo.class, new Object[]{localNode.getId()});
		} catch (DaoException e) {
			throw new DaoException(e);
		}
	}
}